package cn.edu.hunau.thread;

import java.util.concurrent.TimeUnit;

/**取款的线程
 * @author hb
 * @date 2025-08-18 14:42:39
 */
public class DrawThread extends Thread {
    private Account account;
    private double drawAmount;

    public DrawThread( String name,Account account) {
        super(name);
        this.account = account;
    }

    public DrawThread( String name,Account account,double drawAmount) {
        super(name);
        this.account = account;
        this.drawAmount = drawAmount;
    }

    @Override
    public synchronized void run() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //判断账户余额是否充足
            if(this.drawAmount<=account.getBalance()){
                //执行提款
                account.setBalance(account.getBalance()-this.drawAmount);
                System.out.println(this.getName()+"取款成功，余额为："+account.getBalance());
            }else{
                System.out.println(this.getName()+"取款失败，余额不足!");
            }
        }
}
